package com.cskaoyan.weixin.service;

import com.cskaoyan.bean.User1;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.weixin.bean.address.Address;
import com.cskaoyan.weixin.bean.address.AddressDetailVO;
import com.cskaoyan.weixin.bean.address.AddressExample;
import com.cskaoyan.weixin.bean.address.AddressVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<AddressVO> list(Integer id) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        List<AddressVO> addressVOList = addressMapper.selectAddressVO(id);
        for (Address address : addresses) {
            String province = regionMapper.selectNameById(address.getProvinceId());
            String city = regionMapper.selectNameById(address.getCityId());
            String area = regionMapper.selectNameById(address.getAreaId());
            for (AddressVO addressVO : addressVOList) {
                if (addressVO.getId() == address.getId()) {
                    addressVO.setDetailedAddress(province + city + area + address.getAddress());
                    break;
                }
            }
        }
        return addressVOList;
    }

    @Override
    public AddressDetailVO detail(Integer id) {
        AddressDetailVO addressDetailVO = addressMapper.selectByIdForAddressDetailVO(id);
        String provinceName = regionMapper.selectNameById(addressDetailVO.getProvinceId());
        String cityName = regionMapper.selectNameById(addressDetailVO.getCityId());
        String areaName = regionMapper.selectNameById(addressDetailVO.getAreaId());
        addressDetailVO.setProvinceName(provinceName);
        addressDetailVO.setCityName(cityName);
        addressDetailVO.setAreaName(areaName);
        return addressDetailVO;
    }

    @Override
    public int save(Address address) {
        address.setUpdateTime(new Date());
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        address.setUserId(user.getId());
        if (address.getId() == 0){
            address.setAddTime(new Date());
            if (address.getIsDefault() == true){
                addressMapper.updateOtherIsDefault(address.getUserId());
            }
            addressMapper.insert(address);
            return addressMapper.selectLastId();
        } else {
            if (address.getIsDefault() == true){
                addressMapper.updateOtherIsDefault(address.getUserId());
            }
            addressMapper.updateByPrimaryKeySelective(address);
            return address.getId();
        }

    }

    @Override
    public void delete(Integer id) {
        if (id != null && id != 0) {
            addressMapper.deleteByPrimaryKey(id);
        }
    }
}
